﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CMS_Tools
{
    public partial class FileBackup : Form
    {

        string CommerceSolutionFileName;
        string CmsSolutionFileName;

        DirectoryInfo CommerceSolutionDir;
        DirectoryInfo CmsSolutionDir;
        List<string> filesList = new List<string>();

        public FileBackup()
        {
            InitializeComponent();
            FillFilesList();

        }

        private void FillFilesList()
        {
            filesList.Add(@"\Libraries\Nop.Services\Installation\CodeFirstInstallationService_NopCMS.cs");
            filesList.Add(@"\Presentation\Nop.Web\App_Data\Install\InstallationData.xml");
            filesList.Add(@"\Presentation\Nop.Web\App_Data\Localization\Installation\installation.ar.xml");
            filesList.Add(@"\Presentation\Nop.Web\App_Data\Localization\Installation\installation.en.xml");
            filesList.Add(@"\Presentation\Nop.Web\App_Data\Localization\language_pack_ar.xml");
            filesList.Add(@"\Presentation\Nop.Web\App_Data\Localization\nopcms_language_pack_ar.xml");
            filesList.Add(@"\Presentation\Nop.Web\App_Data\Localization\nopcms_language_pack_en.xml");
            filesList.Add(@"\Presentation\Nop.Web\Content\bootstrap\bootstrap.css");
            filesList.Add(@"\Presentation\Nop.Web\Content\bootstrap\bootstrap.min.css");
            filesList.Add(@"\Presentation\Nop.Web\Content\bootstrap\bootstrap-rtl.css");
            filesList.Add(@"\Presentation\Nop.Web\Content\bootstrap\bootstrap-theme.css");
            filesList.Add(@"\Presentation\Nop.Web\Content\bootstrap\bootstrap-theme.min.css");
            filesList.Add(@"\Presentation\Nop.Web\Content\fonts\glyphicons-halflings-regular.eot");
            filesList.Add(@"\Presentation\Nop.Web\Content\fonts\glyphicons-halflings-regular.svg");
            filesList.Add(@"\Presentation\Nop.Web\Content\fonts\glyphicons-halflings-regular.ttf");
            filesList.Add(@"\Presentation\Nop.Web\Content\fonts\glyphicons-halflings-regular.woff");
            filesList.Add(@"\Presentation\Nop.Web\Content\install\style.css");
            filesList.Add(@"\Presentation\Nop.Web\Content\install\style.rtl.css");
            filesList.Add(@"\Presentation\Nop.Web\Views\Install\Index.cshtml");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\affix.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\alert.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\bootstrap-rtl.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\bootstrap.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\bootstrap.min.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\button.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\carousel-rtl.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\collapse.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\dropdown.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\jquery-1.10.2.intellisense.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\jquery-1.10.2.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\jquery-1.10.2.min.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\jquery-1.10.2.min.map");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\modal.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\popover.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\scrollspy.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\tab.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\tooltip.js");
            filesList.Add(@"\Presentation\Nop.Web\Scripts\transition.js");

            filesList.Add(@"\Presentation\Nop.Web\Controllers\InstallController.cs");
        }

        private void btn_CommerceSln_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Solution files (*.sln)|*.sln|All files (*.*)|*.*";


            switch (ofd.ShowDialog())
            {
                case System.Windows.Forms.DialogResult.OK:
                    CommerceSolutionFileName = ofd.FileName;
                    if (!CommerceSolutionFileName.EndsWith("NopCommerce.sln"))
                    {
                        CommerceSolutionFileName = null;
                        lbl_NopSolutionPath.Text = "............................................................................";
                        MessageBox.Show("Please choose the NopCommerce solution file.");
                    }
                    else
                    {
                        lbl_NopSolutionPath.Text = CommerceSolutionFileName;
                    }
                    break;

                case System.Windows.Forms.DialogResult.Cancel:
                    CommerceSolutionFileName = null;
                    lbl_NopSolutionPath.Text = "............................................................................";
                    ofd.Dispose();
                    break;

            }
            BtnEnabledChanged();
        }

        private void btn_CmsSln_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Solution files (*.sln)|*.sln|All files (*.*)|*.*";

            switch (ofd.ShowDialog())
            {
                case System.Windows.Forms.DialogResult.OK:
                    CmsSolutionFileName = ofd.FileName;
                    if (!CmsSolutionFileName.EndsWith("CMS.sln"))
                    {
                        CmsSolutionFileName = null;
                        lbl_CmsSolutionPath.Text = "............................................................................";
                        MessageBox.Show("Please choose the CMS solution file.");
                    }
                    else
                    {
                        lbl_CmsSolutionPath.Text = CmsSolutionFileName;
                    }
                    break;

                case System.Windows.Forms.DialogResult.Cancel:
                    CmsSolutionFileName = null;
                    lbl_CmsSolutionPath.Text = "............................................................................";
                    break;
            }
            BtnEnabledChanged();
        }

        private void BtnEnabledChanged()
        {
            if (!string.IsNullOrEmpty(CommerceSolutionFileName) && !string.IsNullOrEmpty(CmsSolutionFileName))
            {

                CommerceSolutionDir = new DirectoryInfo(Path.GetDirectoryName(CommerceSolutionFileName));
                CmsSolutionDir = new DirectoryInfo(Path.GetDirectoryName(CmsSolutionFileName));

                btn_copy_cms_to_commerce.Enabled = true;
                btn_copy_commerce_to_cms.Enabled = true;
            }
            else
            {
                btn_copy_cms_to_commerce.Enabled = false;
                btn_copy_commerce_to_cms.Enabled = false;
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // this a source file
            btn_copy_cms_to_commerce.Enabled = false;
            btn_copy_commerce_to_cms.Enabled = false;
        }

        private void btn_copy_commerce_to_cms_Click(object sender, EventArgs e)
        {
            foreach (string item in filesList)
            {
                string sourceFilePath = CommerceSolutionDir.FullName + item;
                string destinationPath = CmsSolutionDir.FullName + item;
                Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
                File.Copy(sourceFilePath, destinationPath, true);
            }          
        }

        private void btn_copy_cms_to_commerce_Click(object sender, EventArgs e)
        {
            foreach (string item in filesList)
            {
                string sourceFilePath = CmsSolutionDir.FullName + item;
                string destinationPath = CommerceSolutionDir.FullName + item;
                Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
                File.Copy(sourceFilePath, destinationPath, true);
            } 

        }
    }
}
